#include<iostream>
using namespace std;
#include<cmath>

#include"library.h"

const double x_node[5]={
	-0.906179845938664,
	-0.538469310105683,
	0,
	0.538469310105683,
	0.906179845938664};
const double omega[5]={
	0.236926885056189,
	0.478628670499366,
	0.568888888888889,
	0.478628670499366,
	0.236926885056189};

double Legendre(double x, int n){

	double p1,p2,p3;

	if(n<0){
		cout<<"invalid n for Legendre(x,n), n mustn't be negative.\n";
		exit(1);
	}
	if(fabs(x)>1){
		cout<<"invalid x for Legendre(x,n), please note |x|<=1.\n";
	}
	
	p1 = 1;
	p2 = x;
	if( n==0 )return p1;
	else if( n==1 )return p2;

	for(int i=2;i<=n;i++){
		p3 = x*p2 + (1-1.0/i)*(x*p2-p1);
		p1=p2;
		p2=p3;
	}
	return p3;
}

int n;
double f(double x){
	return pow(x,n);
}

int main(){

	for(int i=0;i<22;i++){
		n=i;
		cout<<"i="<<i<<"\t error="<<GauLegInt(0,1,f,5)-1.0/(n+1)<<endl;
	}

	return 0;
}
